# 5.1
'''
accept返回的新套接字和listen后的套接字有什么区别？
如果把这两种套接字关闭，对客户端的影响分别是什么?
'''
import threading

'''
编写一个 TCP 服务器，在客户端发送数据是 hello时服 务器会随机回复"world"，"haha", "good", "welcome"四个字 符串中其中一个
'''
import socket
import random

my_list = ["world", "haha", "good","welcome"]

def funcA(new_tcp, address):

    num = random.randint(0,3)
    data = my_list[num]
    new_tcp.send(data.encode())
    res = new_tcp.recv(1024)
    if res:
        print(f"当前用户是{address},向你传输了{res.decode()}")
    else:
        print("对方已下线")


def main():
    tcp = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
    tcp.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
    tcp.bind(('',8081))
    tcp.listen(128)
    while True:
        new_tcp, address = tcp.accept()
        threading.Thread(target=funcA,args=(new_tcp, address)).start()
if __name__ == '__main__':
    main()